﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Entities;
using RedHouse.Data.Base;
using RedHouse.Data;
using RedHouse.Data.Common;

namespace RedHouse.AdminControl.UserControls.Customer
{
    public partial class UCSendEmail : System.Web.UI.UserControl
    {
        public string Message { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSend.Click += new EventHandler(btnSend_Click);

            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            Message = string.Empty;
            ErrorPanel.Visible = false;
            SuccessPanel.Visible = false;
        }

        void btnSend_Click(object sender, EventArgs e)
        {
            RedHouseEmail email = GetEmail();
            OperationResult result = EmailService.Instance.SendEmail(email);
            if (!result.Result)
            {
                ErrorPanel.Visible = true;
                SuccessPanel.Visible = false;
            }
            else
            {
                ErrorPanel.Visible = false;
                SuccessPanel.Visible = true;
            }
        }

        private RedHouseEmail GetEmail()
        {
            IList<Subcribe> subcribes = SubcribeService.Instance.GetAllSubcribe();
            List<string> toList = new List<string>();
            List<string> bccList = new List<string>();
            foreach (Subcribe item in subcribes)
	        {
                bccList.Add(item.Email);
	        }

            toList.Add("sonlv00099@gmail.com");

            RedHouseEmail email = new RedHouseEmail()
            {
                 From = "sonlv00099@gmail.com",
                 Password = "Abc@12345",
                 Subject = txtSubject.Text,
                 Body = hdfBody.Value,
                 ToList = toList,
                 CcList = new List<string>(),
                 BccList = bccList
            };
            return email;
        }
    }
}
